<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveSupport::LogSubscriber::TestHelper</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveSupport::LogSubscriber::TestHelper 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../../files/activesupport/lib/active_support/log_subscriber/test_helper_rb.html">activesupport/lib/active_support/log_subscriber/test_helper.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p>Provides some helpers to deal with testing log subscribers by setting up
notifications. Take for instance Active Record subscriber tests:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">SyncLogSubscriberTest</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">TestCase</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">LogSubscriber</span><span class="ruby-operator">::</span><span class="ruby-constant">TestHelper</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">setup</span>
    <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">LogSubscriber</span>.<span class="ruby-identifier">attach_to</span>(:<span class="ruby-identifier">active_record</span>)
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">test_basic_query_logging</span>
    <span class="ruby-constant">Developer</span>.<span class="ruby-identifier">all</span>.<span class="ruby-identifier">to_a</span>
    <span class="ruby-identifier">wait</span>
    <span class="ruby-identifier">assert_equal</span> <span class="ruby-value">1</span>, <span class="ruby-ivar">@logger</span>.<span class="ruby-identifier">logged</span>(:<span class="ruby-identifier">debug</span>).<span class="ruby-identifier">size</span>
    <span class="ruby-identifier">assert_match</span>(<span class="ruby-regexp">/Developer Load/</span>, <span class="ruby-ivar">@logger</span>.<span class="ruby-identifier">logged</span>(:<span class="ruby-identifier">debug</span>).<span class="ruby-identifier">last</span>)
    <span class="ruby-identifier">assert_match</span>(<span class="ruby-regexp">/SELECT \* FROM &quot;developers&quot;/</span>, <span class="ruby-ivar">@logger</span>.<span class="ruby-identifier">logged</span>(:<span class="ruby-identifier">debug</span>).<span class="ruby-identifier">last</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>All you need to do is to ensure that your log subscriber is added to
Rails::Subscriber, as in the second line of the code above. The test
helpers are responsible for setting up the queue, subscriptions and turning
colors in logs off.</p>

<p>The messages are available in the @logger instance, which is a logger with
limited powers (it actually does not send anything to your output), and you
can collect them doing @logger.logged(level), where level is the level used
in logging, like info, debug, warn and so on.</p>

    </div>
  


  


  
  


  
    <!-- Namespace -->
    <div class="sectiontitle">Namespace</div>
    <ul>
      
        <li>
          <span class="type">CLASS</span>
          <a href="TestHelper/MockLogger.html">ActiveSupport::LogSubscriber::TestHelper::MockLogger</a>
        </li>
      
    </ul>
  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>S</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="TestHelper.html#method-i-set_logger">set_logger</a>,
              </li>
            
              
              <li>
                <a href="TestHelper.html#method-i-setup">setup</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>T</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="TestHelper.html#method-i-teardown">teardown</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>W</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="TestHelper.html#method-i-wait">wait</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-set_logger">
            
              <b>set_logger</b>(logger)
            
            <a href="TestHelper.html#method-i-set_logger" name="method-i-set_logger" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Overwrite if you use another logger in your log subscriber.</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier">logger</span>
  <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">logger</span> = <span class="ruby-ivar">@logger</span>
<span class="ruby-keyword">end</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-set_logger_source')" id="l_method-i-set_logger_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/f89fa2ae71f8a0496b84793b2a2ad205127f3102/activesupport/lib/active_support/log_subscriber/test_helper.rb#L99" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-set_logger_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/log_subscriber/test_helper.rb, line 99</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">set_logger</span>(<span class="ruby-identifier">logger</span>)
  <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">LogSubscriber</span>.<span class="ruby-identifier">logger</span> = <span class="ruby-identifier">logger</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-setup">
            
              <b>setup</b>()
            
            <a href="TestHelper.html#method-i-setup" name="method-i-setup" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-setup_source')" id="l_method-i-setup_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/f89fa2ae71f8a0496b84793b2a2ad205127f3102/activesupport/lib/active_support/log_subscriber/test_helper.rb#L36" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-setup_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/log_subscriber/test_helper.rb, line 36</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">setup</span>
  <span class="ruby-ivar">@logger</span>   = <span class="ruby-constant">MockLogger</span>.<span class="ruby-identifier">new</span>
  <span class="ruby-ivar">@notifier</span> = <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Notifications</span><span class="ruby-operator">::</span><span class="ruby-constant">Fanout</span>.<span class="ruby-identifier">new</span>

  <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">LogSubscriber</span>.<span class="ruby-identifier">colorize_logging</span> = <span class="ruby-keyword">false</span>

  <span class="ruby-ivar">@old_notifier</span> = <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Notifications</span>.<span class="ruby-identifier">notifier</span>
  <span class="ruby-identifier">set_logger</span>(<span class="ruby-ivar">@logger</span>)
  <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Notifications</span>.<span class="ruby-identifier">notifier</span> = <span class="ruby-ivar">@notifier</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-teardown">
            
              <b>teardown</b>()
            
            <a href="TestHelper.html#method-i-teardown" name="method-i-teardown" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-teardown_source')" id="l_method-i-teardown_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/f89fa2ae71f8a0496b84793b2a2ad205127f3102/activesupport/lib/active_support/log_subscriber/test_helper.rb#L47" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-teardown_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/log_subscriber/test_helper.rb, line 47</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">teardown</span>
  <span class="ruby-identifier">set_logger</span>(<span class="ruby-keyword">nil</span>)
  <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Notifications</span>.<span class="ruby-identifier">notifier</span> = <span class="ruby-ivar">@old_notifier</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-wait">
            
              <b>wait</b>()
            
            <a href="TestHelper.html#method-i-wait" name="method-i-wait" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Wait notifications to be published.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-wait_source')" id="l_method-i-wait_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/f89fa2ae71f8a0496b84793b2a2ad205127f3102/activesupport/lib/active_support/log_subscriber/test_helper.rb#L90" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-wait_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/log_subscriber/test_helper.rb, line 90</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">wait</span>
  <span class="ruby-ivar">@notifier</span>.<span class="ruby-identifier">wait</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    